package com.zjdiepu.www.biz.dh;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.dh.DhAdvanceSublistMapper;
import com.zjdiepu.www.ibiz.dh.IDhAdvanceSublistService;
import com.zjdiepu.www.model.dh.dto.DhAdvanceSublist;
import com.zjdiepu.www.model.dh.ext.DhAdvanceSublistExt;

/**
 * Service层操作实现<br/>
 * @author zhangzhengjian<br/>
 * @time 2017年06月21日 09时30分<br/>
 * @since 1.0<br/>
 */
@Service
public class DhAdvanceSublistServiceImpl extends BaseService implements IDhAdvanceSublistService {
	public static final Logger logger = LoggerFactory.getLogger(DhAdvanceSublistServiceImpl.class);

	@Autowired
	private DhAdvanceSublistMapper dhAdvanceSublistMapper;
	
	@Override
	public void saveUpdateAndDelete(DhAdvanceSublist dhAdvanceSublist, OperateType operType) {
		switch (operType) {
		case add:
			dhAdvanceSublist.initInsertData(dhAdvanceSublist.getModifier(), dhAdvanceSublist.getModifierName());
			dhAdvanceSublistMapper.insert(dhAdvanceSublist);
			break;
		case edit:
			dhAdvanceSublist.initUpdateData(dhAdvanceSublist.getModifier(), dhAdvanceSublist.getModifierName());
			// 影响的记录数
			int updateCount = dhAdvanceSublistMapper.updateByPrimaryKeySelective(dhAdvanceSublist);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			dhAdvanceSublist.initDeleteData(dhAdvanceSublist.getModifier(), dhAdvanceSublist.getModifierName());
			// 影响的记录数
			int deleteCount = dhAdvanceSublistMapper.updateByPrimaryKeySelective(dhAdvanceSublist);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<DhAdvanceSublist> selectByExt(DhAdvanceSublistExt dhAdvanceSublistExt) {
		return dhAdvanceSublistMapper.selectByExt(dhAdvanceSublistExt);
	}
	
	@Override
	public int countByExt(DhAdvanceSublistExt dhAdvanceSublistExt){
		return dhAdvanceSublistMapper.countByExt(dhAdvanceSublistExt);
	}
	
	@Override
	public DhAdvanceSublist selectById(String id) {
		return dhAdvanceSublistMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			dhAdvanceSublistMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(DhAdvanceSublist dhAdvanceSublist) {
		return dhAdvanceSublistMapper.countRefined(dhAdvanceSublist);
	}

	@Override
	public DhAdvanceSublist selectRefined(DhAdvanceSublist dhAdvanceSublist) {
		return dhAdvanceSublistMapper.selectRefined(dhAdvanceSublist);
	}

	@Override
	public boolean repeat(DhAdvanceSublist dhAdvanceSublist) {
		int count = countRefined(dhAdvanceSublist);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(dhAdvanceSublist.getId())) {
			return true;
		}
		DhAdvanceSublist dbItem = selectRefined(dhAdvanceSublist);
		if(null == dbItem) {
			return false;
		}
		return !dhAdvanceSublist.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return dhAdvanceSublistMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
}
